<?php

namespace App\Http\Controllers;

use App\Models\Answer;
use App\Models\Question;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AnswersController extends Controller
{


    /**
     * AnswersController constructor.
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function store($questionId)
    {
        $question = Question::published()->findOrFail($questionId);

        $this->validate(request(), [
            'content' => 'required',
        ]);
        $question->addAnswer(['user_id'=> auth()->id(),'content'=> request('content')]);
        return back();
    }

    public function destroy(Answer $answer)
    {
        $this->authorize('delete',$answer);
        $answer->delete();
        return back();
    }
}
